package com.example.myapplication;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.adapter.VideoListAdapter;
import com.example.myapplication.utils.Request;
import com.example.myapplication.utils.UICache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.song.videoplayer.DemoQSVideoView;
import org.song.videoplayer.IVideoPlayer;
import org.song.videoplayer.PlayListener;

public class MyMediaPlayer extends AppCompatActivity {
    private boolean isPrepare = false;
    private DemoQSVideoView qsVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_my_media_player);
        new InitUI(this).execute();

    }
    @Override
    public void onBackPressed() {
        int currentMode = qsVideoView.getCurrentMode();
        if (qsVideoView.getCurrentMode()==101){
            qsVideoView.quitWindowFullscreen();
        }else {
            super.onBackPressed();
            qsVideoView.release();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (isPrepare && !qsVideoView.isPlaying()) {
            qsVideoView.play();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        qsVideoView.pause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        qsVideoView.release();
    }
    private  class InitUI extends AsyncTask<String, Integer, JSONArray> {
        private Context context;
        InitUI (Context context){
            this.context=context;
        }
        @Override
        protected JSONArray doInBackground(String... strings) {
            String classify = getIntent().getSerializableExtra("classify").toString();
            JSONArray jsonarr = Request.jsonarr("http://101.132.147.216/paa/videolist?classify=" + classify);
            return jsonarr;
        }

        @Override
        protected void onPostExecute(JSONArray jsonarr) {
            //DemoQSVideoView的ui用的jc播放器
            qsVideoView = (DemoQSVideoView) findViewById(R.id.xxx);
            try {
                JSONObject o = jsonarr.getJSONObject(0);
                if (!o.getString("url").contains("http")){
                    o.put("url","http://["+ UICache.v6+"]"+o.getString("url"));
                }
                qsVideoView.setUp(o.getString("url"),o.getString("name") );
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //设置多个清晰度和ijk配置
            //List<IjkMedia.Option> list = new ArrayList<>();
            //list.add(new IjkMedia.Option(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 1));
            //demoVideoView.setUp(
            //QSVideo.Build(url).title("这是标清标题").definition("标清").resolution("标清 720P").build(),
            //QSVideo.Build(url).title("这是高清标题").definition("高清").resolution("高清 1080P").option(list).build());
            //qsVideoView.getCoverImageView().setImageResource(R.mipmap.cover);//封面
            //设置监听
            qsVideoView.setPlayListener(new PlayListener() {
                @Override
                public void onStatus(int status) {//播放器的ui状态
                    if (status == IVideoPlayer.STATE_AUTO_COMPLETE)
                        qsVideoView.quitWindowFullscreen();//播放完成退出全屏
                }
                @Override//全屏/普通/浮窗...
                public void onMode(int mode) {

                }
                @Override//播放事件 初始化完成/缓冲/出错/点击事件...
                public void onEvent(int what, Integer... extra) {

                }

            });
            //进入全屏的模式 0横屏 1竖屏 2传感器自动横竖屏 3根据视频比例自动确定横竖屏      -1什么都不做
            qsVideoView.enterFullMode=3;
            qsVideoView.play();
            isPrepare=true;

            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recy_video_list);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);//禁止滑动 重写canScrollHorizontally返回false
            recyclerView.setLayoutManager(layoutManager);
            VideoListAdapter adapter = new VideoListAdapter(jsonarr, qsVideoView);
            recyclerView.setAdapter(adapter);
            recyclerView.addItemDecoration(new DividerItemDecoration(getBaseContext(), DividerItemDecoration.VERTICAL));
            adapter.setGetListener(new VideoListAdapter.GetListener() {
                @Override
                public void onClick(int position) {
                    adapter.setmPosition(position);
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }
}